﻿using System;
using System.Web.Mvc;
using Moq;
using NUnit.Framework;
using Safal.IncidentTracker.Domain.Abstract;
using Safal.IncidentTracker.Domain.Entities;
using Safal.IncidentTracker.WebUI.Controllers;
using Safal.IncidentTracker.WebUI.Models;

namespace Safal.IncidentTracker.Tests
{
    [TestFixture]
    public class Search
    {
        [Test]
        public void Empty_Search_Returns_List()
        {
            // Arrange
            var controller = new SearchController(null);

            // Act
            var result = controller.Search(string.Empty);

            // Assert
            result.ShouldBeRedirectionTo(new { controller = "Incidents", action = "List", page = 1 });
        }
        [Test]
        public void Search_Returns_Search_View_With_Matched_Items()
        {
            // Arrange
            Mock<IIncidentsRepository> repository = UnitTestHelpers.MockIncidentsRepository(
                  new Incident { Id = 1, Title = "Cars" },
                  new Incident { Id = 2, Title = "Lego" }
            );
            var controller = new SearchController(repository.Object);
            const string searchTerm = "Cars";

            // Act
            var result = controller.Search(searchTerm);

            // Assert
            var viewModel = (SearchResultsViewModel)(((ViewResult)result).ViewData.Model);

            repository.Verify(r => r.Incidents, Times.Once());
            Assert.That(viewModel.Incidents.Count, Is.EqualTo(1));
            Assert.That(viewModel.Incidents[0].Title, Is.EqualTo("Cars"));
            Assert.That(viewModel.Incidents[0].Id, Is.EqualTo(1));
            result.ShouldBeView(string.Empty);
        }

        [Test]
        public void Advanced_Search_Displays_Enhanced_Items()
        {
            // Arrange
            var controller = new SearchController(null);

            // Act
            var result = controller.AdvancedSearch(null);

            // Assert
            result.ShouldBeView("AdvancedSearch");
        }
        [Test]
        public void Advanced_Search_Returns_View_With_Matched_Items() 
        {
            // Arrange
            Mock<IIncidentsRepository> repository = UnitTestHelpers.MockIncidentsRepository(
                  new Incident { Id = 1, Title = "Broken Arm" },
                  new Incident { Id = 2, Title = "Sprained Ankle" },
                  new Incident { Id = 3, Title = "Eye Injury", DateOfIncident = DateTime.Parse("27/08/2001")},
                  new Incident { Id = 4, Title = "Eye Injury #2", DateOfIncident = DateTime.Parse("17/02/2010"), InjuryLocation = "Eye"},
                  new Incident { Id = 5, Title = "Eye Injury #3", DateOfIncident = DateTime.Parse("17/03/2011")}
            );
            var controller = new SearchController(repository.Object);

            // Act
            var search = new AdvancedSearch { Query = "eye injury", DateFrom = DateTime.Parse("16/02/2010"), DateTo = DateTime.Parse("19/02/2010") , InjuryLocation = "eye"};
            var result = controller.AdvancedSearch(search);

            // Assert
            var viewModel = (SearchResultsViewModel)(((ViewResult)result).ViewData.Model);

            repository.Verify(r => r.Incidents, Times.Once());
            Assert.That(viewModel.Incidents.Count, Is.EqualTo(1));
            Assert.That(viewModel.Incidents[0].Title, Is.EqualTo("Eye Injury #2"));
            Assert.That(viewModel.Incidents[0].Id, Is.EqualTo(4));
            result.ShouldBeView("Search");
        }
    }
}
